package com.itheima.test5case2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

public class MyCallable implements Callable<ArrayList<Integer>> {
    //奖池
    ArrayList<Integer> list = new ArrayList<>();

    public MyCallable() {
        //准备好奖池、两个抽奖箱共用一个奖池
        Collections.addAll(list, 10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300, 700);
    }

    @Override
    public ArrayList<Integer> call() throws Exception {
        //每一个线程进来都创建一个记录自己抽奖情况的集合
        ArrayList<Integer> myList = new ArrayList<>();
        while (true) {
            synchronized (MyCallable.class) {
                if (list.size() == 0) {
                    //System.out.println("奖都被抽完了");
                    break;
                } else {
                    //随机
                    Collections.shuffle(list);
                    //将抽奖结果添加到各自抽奖箱
                    myList.add(list.get(0));
                    //删除抽取到的奖项
                    list.remove(0);
                }
            }
            Thread.sleep(10);
        }
        return myList;
    }
}
